//
//  SuspensionView.m
//  NewDaliy
//
//  Created by xuPeng on 2017/8/25.
//  Copyright © 2017年 陈旭鹏. All rights reserved.
//

#import "SuspensionView.h"
#import <pop/POP.h>

@interface SuspensionView ()

@property (nonatomic, strong) UIButton *suspensionButton;

@end


@implementation SuspensionView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addButton:frame];
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self customUI];
}
- (void)addButton:(CGRect)frame{
    
    UIButton *suspensionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [suspensionBtn setImage:[UIImage imageNamed:@"c_Qdaily button_54x54_"] forState:UIControlStateNormal];
    
    [suspensionBtn addTarget:self action:@selector(clickBtn:) forControlEvents:6];
    [self addSubview:suspensionBtn];
    self.suspensionButton = suspensionBtn;

}
- (void)customUI{

    self.suspensionButton.tag = self.SuspensionButtonStyle;
    [_suspensionButton setImage:[UIImage imageNamed:@"c_Qdaily button_54x54_"] forState:UIControlStateNormal];
}


- (void)clickBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (_suspensionButton.tag == SuspensionButtonStyleQType || _suspensionButton.tag == SuspensionButtonStyleCloseType) {
        //加判断，防止连击悬浮按钮是出现界面逻辑混乱交互的情况
        if(0 == sender.layer.frame.origin.y){
            
            [UIView animateWithDuration:0.05 animations:^{
                [self suspensionButtonAnimationWithOffsetY:80];
             } completion:^(BOOL finished){
                 [self popAnimationWithOffset:-80 beginTime:0];
//                 弹出菜单界面
                 if (_suspensionButton.tag == SuspensionButtonStyleQType) {
                     [sender setImage:[UIImage imageNamed:@"c_close button_54x54_"] forState:UIControlStateNormal];
                     //重新设置悬浮按钮的tag
                     self.suspensionButton.tag = SuspensionButtonStyleCloseType;
                     if (self.delegate && [self.delegate respondsToSelector:@selector(popUpMenuView)]) {
                         [self.delegate popUpMenuView];
                     }
                     return;
                     //因为上面已经对点击事件做处理了，且更改了suspensionButton的tag，此处如果return就会继续执行下面的方法，导致逻辑错误。
                 }
                 if (_suspensionButton.tag == SuspensionButtonStyleCloseType) {
                     [sender setImage:[UIImage imageNamed:@"c_Qdaily button_54x54_"] forState:UIControlStateNormal];
                     //重新设置悬浮按钮的tag
                     self.suspensionButton.tag = SuspensionButtonStyleQType;
                     if (self.delegate && [self.delegate respondsToSelector:@selector(closeMenuView)]) {
                         [self.delegate closeMenuView];
                     }

                     return;
                 }
             }];
        
            
            
             }
        
        
        
    }
}
/** pop动画
 *  POPPropertyAnimation    动画属性
 *  springBounciness:4.0    [0-20] 弹力 越大则震动幅度越大
 *  springSpeed     :12.0   [0-20] 速度 越大则动画结束越快
 *  dynamicsTension :0      拉力  接下来三个都跟物理力学模拟相关 数值调整起来比较麻烦、费时
 *  dynamicsFriction:0      摩擦力
 *  dynamicsMass    :0      质量
 */
 - (void)popAnimationWithOffset:(CGFloat)offset beginTime:(CGFloat)beginTime {
     POPSpringAnimation *popSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
     popSpring.toValue = @(self.suspensionButton.center.y + offset);
     popSpring.beginTime = CACurrentMediaTime() + beginTime;
     popSpring.springBounciness = 10.0f;
     popSpring.springSpeed = 18;
     [self.suspensionButton pop_addAnimation:popSpring forKey:@"positionY"];
             }
//  改变悬浮按钮Y值
- (void)suspensionButtonAnimationWithOffsetY:(CGFloat)offsetY {
    CGRect tempFrame = self.suspensionButton.layer.frame;
    tempFrame.origin.y += offsetY;
    self.suspensionButton.layer.frame = tempFrame;

}
    
@end
